﻿    using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FacadeSingTowerMall;
using MVCSingTowerMall.Models;
using System.Data.Entity;
using DTOSingTowerMall;

namespace MVCSingTowerMall.Controllers
{
    public class StoreController : Controller
    {
        StoreFacade repo;
        IQueryable<ISubCategory> subCategoryList;
        IQueryable<IStore> stores;
        SearchModel s;

        public StoreController()
        {
            repo = new StoreFacade();
        }

        [HttpPost]
        public ActionResult Store(SearchModel s, string SearchButton)
        {
            //System.Diagnostics.Debug.WriteLine("Post of s.CategoryName_Out:" + s.CategoryName);
            //System.Diagnostics.Debug.WriteLine("Post of s.CategoryID_Out:" + s.CategoryID);
            //System.Diagnostics.Debug.WriteLine("Post of s.SubCategoryID_Out:" + s.SubCategoryID);
            //System.Diagnostics.Debug.WriteLine("Post of s.Level_Out:" + s.Level);
            if (s.StoreName != null)   /*NameSearchView*/
            {
                System.Diagnostics.Debug.WriteLine("hhhhhh:" + s.StoreName);
                stores = repo.getStoresByName(s.StoreName);
                //ViewData["StoreName"] = stores;
                s.StoreList = stores;
                if (Request.IsAjaxRequest())
                {
                    return View("SearchResult", s.StoreList);
                }
                else
                {
                    return View(s);
                }
            }
            else
            {
                if (s.Level > 0)   /*levelview*/    //There are two kind of levelview, one is for store directory, the other is in retail & dining 
                {
                    if (s.CategoryName == null)   //levelview for store dirctory
                    {
                        System.Diagnostics.Debug.WriteLine("http-post 11111111111111111111111");
                        s.StoreList = repo.getStoresByLevel(s.Level);    
                        if (Request.IsAjaxRequest())
                        {
                            System.Diagnostics.Debug.WriteLine("http-post 1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1");
                            return View("SearchResult", s.StoreList);
                        }
                        else
                        {
                            System.Diagnostics.Debug.WriteLine("http-post 1-2-1-2-1-2-1-2-1-2-1-2-1-2");
                            return View(s);
                        }
                    }
                    else  //levelview for retail & dining
                    {
                        System.Diagnostics.Debug.WriteLine("http-post 22222222222222222222222222222222");
                        s.StoreList = repo.getStoresByLevelAndCategory(s.Level, s.CategoryName);  
                        if (Request.IsAjaxRequest())
                        {
                            System.Diagnostics.Debug.WriteLine("http-post 2-1-2-1-2-1-2-1-2-1-2-1-2-1-2");
                            return View("SearchResult", s.StoreList);
                        }
                        else
                        {
                            System.Diagnostics.Debug.WriteLine("http-pos 2-2-2-2-2-2-2-2-2-2-2-2-2-2");
                            return View(s);
                        }
                    }
                }
                else if (s.SubCategoryID > 0)  /*view for different subcategories*/
                {
                    System.Diagnostics.Debug.WriteLine("22222222222222222222222222222222");
                    stores = repo.getStores(s.SubCategoryID, s.CategoryID);
                    s.StoreList = stores;                                                 
                    if (Request.IsAjaxRequest())
                    {
                        System.Diagnostics.Debug.WriteLine("2-1-2-1-2-1-2-1-2-1-2-1-2-1-2");
                        return View("SearchResult", s.StoreList);
                    }
                    else
                    {
                        System.Diagnostics.Debug.WriteLine("2-2-2-2-2-2-2-2-2-2-2-2-2-2");
                        return View(s);                                                        
                    }
                }
                else   /*viewall */   //There are two kind of viewall, one is in the story directory, the other is in retail and dining.
                {
                    if (s.CategoryName != null)   //viewall  stores for retail and dining
                    {
                        System.Diagnostics.Debug.WriteLine("11111111111111111111111111111");
                        stores = repo.getStoresByCategory(s.CategoryName);
                        s.StoreList = stores;                                                      
                        System.Diagnostics.Debug.WriteLine("1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1");
                        return View("SearchResult", s.StoreList);
                    }
                    else     //viewall for Store Directory
                    {
                        System.Diagnostics.Debug.WriteLine("3333333333333333333333333333");
                        stores = repo.getStores();
                        s.StoreList = stores;                                                  
                        System.Diagnostics.Debug.WriteLine("3-1-3-1-3-1-3-1-3-1-3-1-3-1-3-1");
                        return View("SearchResult", s.StoreList);
                    }
                }
            }
        }

        public ActionResult Store(int? subcategoryID, int? categoryID, string categoryName)
        {
            s = new SearchModel();
            s.SubCategoryID = subcategoryID;
            s.CategoryID = categoryID;
            s.CategoryName = categoryName;
            System.Diagnostics.Debug.WriteLine("s.CategoryName_Out:" + s.CategoryName);
            System.Diagnostics.Debug.WriteLine("s.CategoryID_Out:" + s.CategoryID);
            System.Diagnostics.Debug.WriteLine("s.SubCategoryID_Out:" + s.SubCategoryID);
            subCategoryList = repo.getSubCategoryList(categoryName); //create subcategory store items
            s.SubCategoryList = subCategoryList;
            Session["LevelList"] = new SelectList(repo.getLevel());
            if (s.CategoryName == null)  //vewall for store directory without ajax.
            {
                System.Diagnostics.Debug.WriteLine("333333333333333333333333");
                stores = repo.getStores();
                s.StoreList = stores;
                return View(s);
            }
            else if (s.CategoryName != null && s.SubCategoryID > 0)
            {   
                System.Diagnostics.Debug.WriteLine("11111111111111111111111");
                stores = repo.getStores(s.SubCategoryID, s.CategoryID);
                s.StoreList = stores;    
                return View(s);
            }
            else   //viewall without ajax.
            {
                System.Diagnostics.Debug.WriteLine("222222222222222222222222");
                stores = repo.getStoresByCategory(s.CategoryName);
                s.StoreList = stores;                                                                      
                return View(s);
            }
        }

        [OutputCache(Duration = 300, VaryByParam = "q")]
        public string StoreList(string q)
        {
            
            stores = repo.Stores(q);
            string storeList = "";
            
            foreach (var item in stores)
            {
                storeList += item.StoreName + "\n";
            }
            return storeList;
        }
    }
}